package Year22_Month06.day06_2;

public class 填充每个节点的下一个右侧节点指针_DFS {
    public Node connect(Node root) {
        DFS(root);
        return root;
    }
    public void DFS(Node node){
        if(node == null){
            return;
        }
        //先连接当前节点的左右子树
        if(node.left != null){
            node.left.next = node.right;
            //然后把右子树和下一个对应节点连接起来
            if(node.next != null){
                node.right.next = node.next.left;
            }
        }
        DFS(node.left);
        DFS(node.right);
    }
}
